<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center JNPF-flex-main">
      <div class="student-eval">
        <el-row :gutter="10">
          <el-col :span="6">
            <!-- 年级表 -->
            <SessionForm @handle-eval="evalClick" @handle-grade="gradeClick" @handle-create="createClick"
                         @after-create="afterCreate"
                         ref="session-form"/>
          </el-col>
          <el-col :span="18">
            <!-- 评估版本明细 -->
            <SessionEval ref="session-eval" @refresh="refresh" @item-create="itemCreate" @item-update="itemUpdate"
                         @children-create="childrenCreate"/>
          </el-col>
        </el-row>

        <!-- 父项新增修改 弹窗 form -->
        <VerItemForm ref="ver-item-form" v-if="formVisible" @eval-fresh="evalFresh"/>

        <!-- 子项目-->
        <ChildrenItemForm ref="children-item-form" v-if="sonVisible" @eval-fresh="sonFresh"/>

      </div>
    </div>
  </div>
</template>

<script>
import SessionForm from './components/session-form'
import SessionEval from './components/session-eval'
import VerItemForm from './components/ver-item-form'
import ChildrenItemForm from './components/children-item-form'

import {CreateEvalVer} from '@/api/bt/student-eval/grade'
import {log} from "util";

export default {
  name: "student-eval",
  components: {
    SessionForm,
    SessionEval,
    VerItemForm,
    ChildrenItemForm,
  },
  data() {
    let gradeEval = {}
    return {
      // * 新增，修改，详情页面显示与否
      formVisible: false,
      // * 子项目新增，修改，详情页面显示与否
      sonVisible: false,
      gradeEval,
      //保存创建之后年级id
      afterCreateId: undefined,
      afterCreateVerId: undefined,
      activedVerId: undefined,
    }
  },
  methods: {
    //点击当前版本 激活版本
    evalClick(gradeEval) {
      let flag = true
      this.gradeEval = gradeEval;
      this.$nextTick(() => {
        this.$refs['session-eval'].init(gradeEval, flag)
      })
    },
    //点击年级 最新版本
    gradeClick(gradeEval) {
      let flag = false
      this.gradeEval = gradeEval;
      this.$nextTick(() => {
        this.$refs['session-eval'].init(gradeEval, flag)
      })
    },
    //点击创建版本
    createClick(gradeEval) {
      this.afterCreateId = gradeEval.id
      let eval_ver = {
        evalId: gradeEval.id,
        name: gradeEval['lastVerName'],
      }
      CreateEvalVer(eval_ver).then((res) => {
        this.$message({
          message: res.msg,
          type: 'success',
          duration: 1500,
        })
        this.$refs['session-form'].getGradeList(true);
      })
    },
    //创建版本后 获取最新id给 eval 页面初始化
    afterCreate(data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].id === this.afterCreateId) {
          this.afterCreateVerId = data[i]['lastVerId'];
          this.activedVerId = data[i]['verId']
        }
      }
      let grade = {
        id: this.afterCreateId,
        lastVerId: this.afterCreateVerId,
        verId: this.activedVerId,
      }
      this.$refs['session-eval'].init(grade, false)
    },
    //刷新页面
    refresh(isRefresh, gradeRe) {
      this.$refs['session-form'].getGradeList(false);
      if (isRefresh) {
        let reEval = {
          id: this.gradeEval.id,
          name: this.gradeEval.name,
          verId: gradeRe.id,
        }
        this.$refs['session-eval'].init(reEval, isRefresh)
      }
    },
    //新建目录
    itemCreate(verId) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs['ver-item-form'].init(verId, undefined)
      })
    },
    //编辑目录
    itemUpdate(row) {
      if (row.children) {
        this.formVisible = true
        this.$nextTick(() => {
          this.$refs['ver-item-form'].init(undefined, row.id)
        })
      } else {
        console.log(row)
        this.sonVisible = true
        this.$nextTick(() => {
          this.$refs['children-item-form'].init(row, undefined, row.id)
        })
      }
    },

    // * 新增，修改页面显示关闭，新增，修改后刷新
    evalFresh(isRefresh, verId) {
      this.formVisible = false
      if (isRefresh) {
        this.$refs['session-eval'].freshEval(verId)
      } //表格刷新方法
    },
    sonFresh(isRefresh, verId) {
      this.sonVisible = false
      if (isRefresh) {
        this.$refs['session-eval'].freshEval(verId)
      } //表格刷新方法
    },
    //子项 创建
    childrenCreate(row, verId) {
      // console.log(row, verId)
      this.sonVisible = true
      this.$nextTick(() => {
        this.$refs['children-item-form'].init(row, verId, undefined)
      })
    },
  }
}
</script>

<style scoped>

</style>
